<template>
  <div>
    <h3>vue递归组件</h3>
    <!--递归组件https://juejin.cn/post/7072536263089192990 -->
    <RootNav :navList="navList" />
    <el-divider></el-divider>
    <!-- 动态组件 -->
    <h3>vue动态组件</h3>
    <div class="demo">
      <button
        v-for="(_, tab) in tabs"
        :key="tab"
        :class="['tab-button', { active: currentTab === tab }]"
        @click="currentTab = tab"
      >
        {{ tab }}
      </button>
      <keep-alive>
        <component :is="tabs[currentTab]" class="tab"></component
      ></keep-alive>
    </div>
    <el-divider></el-divider>
    <h3>vue异步组件配合Suspense组件</h3>
    <!-- <Async /> -->
    <Suspense>
      <template #default>
        <Async />
      </template>
      <template #fallback>
        <p>请稍等...</p>
      </template>
    </Suspense>
    <h3>vue-Teleport传送组件</h3>
    <teleport to="body">
      <!--这里的to后接指定位置-->
      <div v-if="isShow" class="mask">
        <div class="dialog">
          <h3>我是一个弹窗</h3>
          <button @click="isShow = false">关闭弹窗</button>
        </div>
      </div>
    </teleport>
  </div>
</template>

<script setup>
import { ref, defineAsyncComponent } from "vue";
import RootNav from "./RootNav";
import Home from "./Home";
import Posts from "./Posts";
import Archive from "./Archive";
// import Async from "./Async";
const Async = defineAsyncComponent(() => import("./Async"));
const navList = ref([
  {
    name: "一级导航 1",
  },
  {
    name: "一级导航 2",
    children: [
      { name: "二级导航 2-1" },
      {
        name: "二级导航 2-2",
        children: [{ name: "三级导航 2-2-1" }, { name: "三级导航 2-2-2" }],
      },
      { name: "二级导航 2-3" },
    ],
  },
  {
    name: "一级导航 3",
  },
]);
// 动态组件

const currentTab = ref("Home");

const tabs = {
  Home,
  Posts,
  Archive,
};
const isShow = ref(true);
</script>

<style lang="scss" scoped>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}

.tab-button {
  padding: 6px 10px;
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  border: 1px solid #ccc;
  cursor: pointer;
  background: #f0f0f0;
  margin-bottom: -1px;
  margin-right: -1px;
}
.tab-button:hover {
  background: #e0e0e0;
}
.tab-button.active {
  background: #e0e0e0;
}
.tab {
  border: 1px solid #ccc;
  padding: 10px;
}
.mask {
  width: 200px;
  height: 200px;
  background-color: #e0e0e0;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
